package com.atguigu.guilimall.search.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @DESCRIPTION: 初始化线程的四种方式
 * 1）、继承 Thread
 * Thread01 thread01 = new Thread01();
 * thread01.start();
 * 2）、实现 Runnable 接口
 * Runnable01 runnable01 = new Runnable01();
 * new Thread(runnable01).start();
 * 3）、实现 Callable 接口 + FutureTask （可以拿到返回结果，可以处理异常）
 * FutureTask<Integer> integerFutureTask = new FutureTask<>(new Callable01());
 * new Thread(integerFutureTask).start();
 * // 阻塞等待 需要线程执行完 才会有结果
 * try {
 * Integer integer = integerFutureTask.get();
 * log.info("返回值为{}", integer);
 * } catch (Exception e) {
 * throw new RuntimeException(e);
 * }
 * log.info("main.....end....");
 * 4）、线程池
 * @author: zr
 * @DATE: 2022/12/24 15:53
 */
@Slf4j
public class ThreadTest {
    public static ExecutorService service = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        log.info("main.....start....");
//        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
//            log.info("当前线程:" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            log.info("运行结果:{}", i);
//            return i;
//        }, service).whenComplete((result, exception) -> {
//            // 能得到异常信息 不能修改返回结果
//            log.info("异步任务成功完成，结果是" + result + "返回异常是：" + exception);
//        }).exceptionally((exception) -> 10);
//        Integer integer = completableFuture.get();
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            log.info("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            log.info("运行结果:{}", i);
            return i;
        }, service).handle((res, exception) -> {
            if (res != null) {
                return res * 2;
            }
            if (exception != null) {
                return 0;
            }
            return 0;
        });
        Integer integer = completableFuture.get();
        log.info("main.....end...."+integer);

    }

    public static void thread(String[] args) {
        log.info("main.....start....");
//        Thread01 thread01 = new Thread01();
//        thread01.start();
//        Runnable01 runnable01 = new Runnable01();
//        new Thread(runnable01).start();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                200,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(10000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        service.execute(new Runnable01());
        log.info("main.....end....");
    }

    public static class Thread01 extends Thread {
        @Override
        public void run() {
            log.info("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            log.info("运行结果:{}", i);
        }
    }

    public static class Runnable01 implements Runnable {
        @Override
        public void run() {
            log.info("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            log.info("运行结果:{}", i);
        }
    }

    /**
     * 实现Callable 的方式 有返回值
     */
    public static class Callable01 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            log.info("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            log.info("运行结果:{}", i);
            return i;
        }
    }
}
